﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XjjXmm.CodeGenerator
{
    interface IGeneratorStrangy
    {
        dynamic GetTableModel(TableInfo table);
    }

    public class NetBaseGeneratorStrangy : IGeneratorStrangy
    {
        public virtual dynamic GetTableModel(TableInfo table)
        {
            // table.PropertyName = GetTableName(table);


            var columns = table.ColumnInfos.Where(t =>
            {
                return !",IsDeleted,CreatedUserId,CreatedUserName,CreatedTime,ModifiedUserId,ModifiedUserName,ModifiedTime,".Contains("," + t.DbColumnName + ",");

            }).Select(t => new
            {
                DbColumnName = t.DbColumnName,
                DataType = GetType(t),
                ColumnDescription = t.ColumnDescription,
                IsNullable = t.IsNullable,
                IsIdentity = t.IsIdentity,
                IsPrimarykey = t.IsPrimarykey,
                ColunmHeader = GetHeader(t),
                PropertyColumnName = GetPrpertyName(t)
            });

            return new
            {
                DbName = table.DbName,
                PropertyName = GetTableName(table),
                Description = table.Description,
                BaseClass = GetSuperClass(table),
                ColumnInfos = columns
            };
        }

        protected virtual string ConverCamlName(string field)
        {
            var names = field.ToLower().Split('_');

            var res = "";
            foreach (var name in names)
            {
                res += name.Substring(0, 1).ToUpper() + name.Substring(1);
            }

            return res;
        }
        protected virtual string GetTableName(TableInfo table)
        {

            return ConverCamlName(table.DbName) + "Entity";
        }

        protected virtual string GetType(ColumnInfo column)
        {
            var res = column.DataType.ToLower() switch
            {
                "int" => "int",
                "bigint" => "long",
                "varchar" => "string",
                "char" => "string",
                "text" => "string",
                "enum" => "string",
                "datetime" => "DateTime",
                "bit" => "bool",

                _ => "",
            };

            if (column.IsNullable)
            {
                res += "?";
            }

            return res;

        }

        protected virtual string GetPrpertyName(ColumnInfo column)
        {
            return column.DbColumnName;
        }

        protected virtual string GetHeader(ColumnInfo column)
        {
            var sb = new StringBuilder();
            sb.Append($"[SugarColumn(ColumnName = \"{column.DbColumnName}\"");
            if (column.IsPrimarykey)
            {
                sb.Append(", IsPrimaryKey = true");
            }

            if (column.IsIdentity)
            {
                sb.Append(", IsIdentity =true");
            }

            sb.Append(")]");

            return sb.ToString();
        }

        protected virtual string GetSuperClass(TableInfo table)
        {
            if (table.ColumnInfos.FirstOrDefault(t => t.DbColumnName == "IsDeleted") != null)
            {
                return " : EntityFull";
            }

            return ": EntityAdd";
        }
    }

  
}
